package com.wzt.car.service.impl;

import com.wzt.base.constant.BaseConstants;
import com.wzt.base.service.impl.BaseServiceImpl;
import com.wzt.car.domain.Type;
import com.wzt.car.mapper.TypeMapper;
import com.wzt.car.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Zenith
 */
@Service
public class SpringCacheImpl extends BaseServiceImpl<Type> implements ITypeService {
    @Autowired
    private TypeMapper typeMapper;

    @Override
    @Cacheable(cacheNames = BaseConstants.CAR_TYPE, key = "'ALL'")
    public List<Type> tree(Long pid) {
        List<Type> children = typeMapper.loadChildrenByPid(pid);
        // 递归结束条件
        if (children == null) {
            return null;
        }

        // 递归调用，设置所有的子节点
        for (Type child : children) {
            List<Type> grandson= tree(child.getId());
            child.setChildren(grandson);
        }
        return children;
    }

    @Override
    @CacheEvict(cacheNames = BaseConstants.CAR_TYPE,key = "'ALL'")
    public void add(Type type) {
        super.addOne(type);
    }

    @Override
    @CacheEvict(cacheNames = BaseConstants.CAR_TYPE,key = "'ALL'")
    public void remove(Long id) {
        super.removeOne(id);
    }

    @Override
    @CachePut(cacheNames = BaseConstants.CAR_TYPE,key = "'ALL'")
    public void edit(Type type) {
        super.editOne(type);
    }
}
